package com.yyn.educms.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyn.R;
import com.yyn.educms.entity.CrmBanner;
import com.yyn.educms.service.CrmBannerService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author yyn
 * @since 2023-01-02
 */
@RestController
@RequestMapping("/educms/bannerAdmin")
public class CrmBannerAdminController {
    @Autowired
    CrmBannerService crmBannerService;
//1、先写完完整的增删改查操作
    @ApiOperation("分页查询")
    @GetMapping("/page/{page}/{pagesum}")
    public R page(int page,int pagesum){
        Page<CrmBanner> crmBannerPage = new Page<>(page,pagesum);
        LambdaQueryWrapper<CrmBanner> crmBannerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        crmBannerLambdaQueryWrapper.orderByDesc(CrmBanner::getSort);
        Page<CrmBanner> page1 = crmBannerService.page(crmBannerPage, crmBannerLambdaQueryWrapper);
//        返回 内容 加 页码
        return R.success().data("items",page1.getRecords()).data("total",page1.getTotal());
    }
//    查所有
@ApiOperation("查所有")
@GetMapping("getAll")
    public R getAll(){
        List<CrmBanner> list = crmBannerService.list();
        return R.success().data("all",list);
    }
    //    根据id查
    @ApiOperation("根据id查")
    @GetMapping("getAll/{id}")
    public R getById(@PathVariable String id){
        CrmBanner one = crmBannerService.getById(id);
        return R.success().data("getById",one);
    }
    @ApiOperation("修改")
    @PostMapping("update/{id}")
    public R update(@RequestBody CrmBanner crmBanner){
        boolean b = crmBannerService.updateById(crmBanner);
        if (b){
            return R.success().message("修改成功");
        }
        return R.error().message("修改失败");
    }
    @ApiOperation("删除")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id){
        boolean b = crmBannerService.removeById(id);
        if (b){
            return R.success().message("删除成功");
        }
        return R.error().message("删除失败");
    }

}

